<?php
/**
 * Description of Form
 *
 * @author josema
 */
class Form {

    /**
     *
     * @param array $data Array, cada index un input del form, compuesto de un
     * array que dara la info para generar el form.
     * 'type' => input type
     * 'id' => input id
     * 'name' => input name
     * 'value' => value of this field
     * 'label' => generates a <label> tag for this input
     * @param string $action Action of the form
     * @param boolean $wrap Indicates if the form is wrapped with <form...></form> or not
     * @return string Form
     */
    static public function generate($data, $action, $wrap = true){
        $tpl = new Template();

        foreach ($data as $key => $value) {
            // If is a select input, format the options ids
            if($value['type'] == 'select')
                $value['options_id'] = array_keys($value['options']);
            
            $tpl->add('input', array($value));
        }

        $tpl->add('action', $action);

        $template_name = ($wrap)? 'form_wrap.tpl': 'form.tpl';
        return $tpl->fetch($template_name);
    }
}
?>
